# Make LaTeX chunk for Table 3

# Load Models and Store AUCs
countries <- c('indo', 'colo')
models <- c('full', "onlyviolence",
            "violpop", "noviolence",
            "fullfixed", "fullvarying")

metrics <- c('mse', 'r2_dev')
performance <- array(NA,
                   dim = c(length(countries),
                           length(metrics),
                           length(models)),
                   dimnames = list(countries,
                                   metrics,
                                   models))
algo <- 'ebma'
for (country in countries) {
  for (x in models) {
      filename <- paste(modeldir,"/",
                    country,
                    "_ebma_",
                    "count",
                    "_", x, ".RData",
                    sep = "")
      load(filename)
      mse_by_year = c()
      rdev_by_year = c()
      for (i in 1:length(get(paste(algo,".results",sep="")))) {
          predictions <- as.vector(get(paste(algo,".results",sep=""))[[i]]$fit.oos)
          realizations <- as.vector(get(paste(algo,".results",sep=""))[[i]]$actual.oos)
          mse_by_year <- c(mse_by_year,
                            mean((realizations - predictions)^2))
          rdev_by_year <- c(rdev_by_year,
                            Rdev(realizations,
                                 predictions))
      }
      performance[country, 'mse', x] <- mean(mse_by_year)
      performance[country, 'r2_dev', x] <- mean(rdev_by_year)
  }
}

# Build Table with Labels
country.labels <- c(indo='(a) Indonesia',
                    colo='(b) Colombia')
metric.labels <- c(mse="MSE",
                   r2_dev="$R^{2}_{dev}$")
model.labels <- c(full='Full\\\\ Predictors',
                  onlyviolence='All Past\\\\ Violence\\\\ Measures',
                  violpop='All Past\\\\ Violence \\\\ \\& Population',
                  noviolence='Full Excl.\\\\ Past\\\\ Violence',
                  fullfixed='Time-\\\\ Invariant\\\\ Predictors',
                  fullvarying='Time-\\\\ Varying\\\\ Predictors')

table <- performance
dimnames(table)[[1]] <- country.labels[dimnames(table)[[1]]]
dimnames(table)[[2]] <- metric.labels[dimnames(table)[[2]]]
dimnames(table)[[3]] <- model.labels[dimnames(table)[[3]]]

# Print to Latex
printA3(table = table,
        filepath = "tables",
        header='Predicting the count of violent events',
        file = "table_A3",
        head.width = '1.8')

